<template>
  <div class="ShopList">
    <el-input v-model="input" placeholder="请输入搜索内容" class="input-with-select" @change="OnSearch">
      <el-button slot="append" icon="el-icon-search" />
    </el-input>
    <el-button type="primary" @click="$router.push('shoplist/addshop')">添加商品</el-button>

    <el-table
      :data="shopList"
      border
      style="width: 100%"
    >
      <el-table-column
        label="#"
        width="50"
        type="index"
      />
      <el-table-column
        prop="goods_name"
        label="商品名称"
        width="740"
      />
      <el-table-column
        prop="goods_price"
        label="商品价格(元)"
      />
      <el-table-column
        prop="goods_weight"
        label="商品重量"
      />
      <el-table-column
        label="创建时间"
        width="140"
      >
        <template slot-scope="{row}">
          {{ row.add_time | formateTime }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="120"
      >
        <template>
          <el-button type="primary" size="mini" icon="el-icon-edit" />
          <el-button type="danger" size="mini" icon="el-icon-delete" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <paging :total="total" @change-pagesize="ChangePageSize" @change-page="ChangePage" />
  </div>
</template>

<script>
import { getShopList } from '@/api/ShopList.js'
import paging from '@/components/paging'
export default {
  name: 'ShopList',
  components: { paging },
  data() {
    return {
      input: '', // 搜索框内容
      form: {
        query: '', // 查询参数
        pagenum: 1, // 当前页码
        pagesize: 5// 每页显示条数
      },
      shopList: [], // 商品列表
      total: 0 // 总条数
    }
  },

  created() {
    this.loadShopList()
  },

  methods: {
    async loadShopList() {
      const res = await getShopList(this.form)
      console.log(res)
      this.shopList = res.data.goods
      this.total = res.data.total
    },
    // 搜索
    OnSearch(val) {
      this.form.query = val
      this.loadShopList()
    },
    // 每页显示数量
    ChangePageSize(e) {
      this.form.pagesize = e
      this.loadShopList()
    },
    // 切换页数
    ChangePage(e) {
      this.form.pagenum = e
      this.loadShopList()
    }
  }
}
</script>

<style scoped lang='scss'>
.ShopList{
  padding: 20px;
  background-color: #fff;
}
.el-input{
  width: 404px;
}
.el-input-group{
  padding-right: 20px;
}
.el-table{
  margin: 15px 0;
}
</style>
